﻿using System.Diagnostics;
using System.Text;
using XA_DATASETLib;

namespace IRUM.TR
{
    public abstract class CBaseQuery
    {
        protected const string InBlockName = "InBlock";
        protected const string OutBlockName = "OutBlock";
        protected const string OutBlock1Name = "OutBlock1";

        protected string m_InBlockName = string.Empty;
        protected string m_OutBlockName = string.Empty;
        protected string m_OutBlock1Name = string.Empty;

        protected TR_Type type;
        protected string classname = string.Empty;

        protected XAQuery m_Query = new XAQuery();


        protected void SetClassData(TR_Type tr_type)
        {
            type = tr_type;
            classname = type.ToString();

            m_Query.ResFileName = @"C:\ETRADE\xingAPI\Res\" + type.ToString() + ".res";
            m_Query.ReceiveData += m_Query_ReceiveData;
            m_Query.ReceiveMessage += m_Query_ReceiveMessage;

            MakeBlockName();
        }

        void m_Query_ReceiveMessage(bool bIsSystemError, string nMessageCode, string szMessage)
        {
            string errormsg = string.Empty;

            if (bIsSystemError)
            {
                errormsg = "[System] ";
            }
            else
            {
                errormsg = "[" + type.ToString() + "] ";
            }

            errormsg += nMessageCode + ", ";
            errormsg += szMessage;

            Debug.Print(errormsg);
        }

        void m_Query_ReceiveData(string szTrCode)
        {
            HandleQuery();
        }

        protected void MakeBlockName()
        {
            m_InBlockName = type.ToString() + InBlockName;
            m_OutBlockName = type.ToString() + OutBlockName;
            m_OutBlock1Name = type.ToString() + OutBlock1Name;
        }

        protected void AddLog(string log)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("[");
            sb.Append(classname);
            sb.Append("]\t");
            sb.Append(log);

            Debug.Print(sb.ToString());
        }

        public abstract void HandleQuery();

        protected abstract void SetRequestData();

        public void Request()
        {
            SetRequestData();
            m_Query.Request(false);
        }

        protected void RequestNext()
        {
            //Next 후 다음 종목으로 넘어가는 부분에 대한 보완이 필요할 것임.
            SetRequestData();
            m_Query.Request(true);
        }
    }
}
